#include "../liolib/lioflat.h"
#define VERTEX glVertex3fv

GLfloat
	LightAmbient[]= { 0.1f, 0.1f, 0.1f, 1.0f }, // Ambient Light Values
	LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f }, // Diffuse Light Values
	LightPosition[]= { 0.0f, 0.0f, 0.0f, 1.0f }, // Light Position
	LightSpecular[]= { 0.1f, 0.1f, 0.1f, 1.0f }, // Light Specular
	LightAttenuate[]= { 1.0f, 0.005f, 0.001f }, // Light Attenuation
	fogColor[4]= {0.0f, 0.0f, 0.0f, 1.0f}; // Fog Color

class AppClassName:public FlatApplication
{
protected:
	BOOL create();
	BOOL glCreate();
	void glDestroy();
	void destroy();
	void reshape ();
	BOOL listen(WORD id, WORD code, const void *info);
	void draw2D();
	void draw3D();
	BOOL input(BYTE type);
	void updateSelection();
	void updateList();
	void update();
	void recalc();
	void capture();
};

void AppClassName::capture()
{
	Image screen;
	screenshot(&screen);
	writeJPEG(_T("screenshot.jpg"),&screen);
}
BOOL AppClassName::create ()				
{
	if(!FlatApplication::create())
		return FALSE;
	name=_T("AppClassName");
	display.width=1024;
	display.height=768;
	display.bpp=32;
	clearColor=INT_WHITE;

	InitInfo info;
	info.read();

	window.left=info.getInt(		_T("left"),window.left);
	window.top=info.getInt(			_T("top"),window.top);
	window.width=info.getInt(		_T("width"),window.width);
	window.height=info.getInt(		_T("height"),window.height);

	DPRINT(_T("\n\n--------------------- Application created.\n"));PRINTMEM();
	return TRUE;
}

void AppClassName::destroy()
{
	InitInfo info;

	info.addInt(	_T("left"),window.left);
	info.addInt(	_T("top"),window.top);
	info.addInt(	_T("width"),window.width);
	info.addInt(	_T("height"),window.height);

	info.write();

	FlatApplication::destroy();
}

BOOL AppClassName::glCreate ()
{
	if(!FlatApplication::glCreate())
		return FALSE;

	DPRINT(_T("\n\n--------------------- Graphics created.\n"));PRINTMEM();
	return TRUE;
}

void AppClassName::glDestroy()
{
	FlatApplication::glDestroy();
}

void AppClassName::reshape()
{
	FlatApplication::reshape();
}

BOOL AppClassName::listen(WORD id, WORD code, const void *info)
{
	return FALSE;
}

void AppClassName::draw2D()
{
}

void AppClassName::draw3D()
{
}

void AppClassName::update()
{
}

BOOL AppClassName::input(BYTE type)
{
	if(FlatApplication::input(type))
		return TRUE;
	switch(type)
	{
		case MOUSE_INPUT_MSG:
		{
			switch(msg.message)
			{
				case WM_LBUTTONDOWN:
				case WM_MOUSEMOVE:
				case WM_MOUSEWHEEL:
				default:
					return FALSE;
			}
			return TRUE;
		}
		break;
		case KEY_INPUT_MSG:
		{
			if(keyboard.pressed)
			{
				if(keyboard.waiting)switch(keyboard.code)
				{
					case VK_ESCAPE:
						terminate();
						break;
					default:
						return FALSE;
				}
				else switch(keyboard.symbol)
				{
					case '+':
					case '=':
						break;
					case '-':
					case '_':
						break;
					default:
						return FALSE;
				}
				return TRUE;
			}
		}
	}
	return FALSE;
}


int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
	Debugger output(_T("debug.txt"));
	BOOL exc=TRUE;
	int result=1,exc_code=0,throw_line=0;
	CSTR exc_msg=NULL,throw_source=NULL;
	try
	{
		AppClassName app;
		DPRINT(_T("\n\n--------------------- Application started.\n"));PRINTMEM();
		result=app.main(hInstance,hPrevInstance,lpCmdLine,nCmdShow);
		exc=FALSE;
	}
	catch(const char * str){exc_msg=str;}
	catch(int n){exc_code=n;}
	catch(...){}
	if(exc)
	{
		DPRINT(	_T("\n\n--------------------- Application intexcupted by an exception!\nError code: %d\nError msg: %s\n"),
				exc_code, exc_msg?exc_msg:"none");
	}
	DPRINT(_T("\n\n--------------------- Application closed.\n"));PRINTMEM(TRUE);
	return result;
}